package com.lq.util.jdbc;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.lq.util.jdbc.LqConfig;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 数据库链接池<br>
 * 默认读取lqjdbc.properties文件<br>
 * @author 吃人的肉
 * QQ:376870344<br>
 * email:liuqingrou@163.com
 */
public class LqDBOperator {
	public static ComboPooledDataSource dsWebgame = null;
	public static LqConfig conf = null;
	static {
		if (conf == null) {
			conf = new LqConfig();
			conf.init("lqjdbc.properties");
		}
		try {
			Class.forName(conf.getString("dataSource.driverClassName"));
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		dsWebgame = new ComboPooledDataSource();
		try {
			dsWebgame.setDriverClass(conf.getString("dataSource.driverClassName"));
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		// 参数由 Config 类根据配置文件读取
		// 设置JDBC的URL
		dsWebgame.setJdbcUrl(conf.getString("dataSource.allurl").split("\\*\\*\\*")[0]);
		// 设置数据库的登录用户名
		dsWebgame.setUser(conf.getString("dataSource.allurl").split("\\*\\*\\*")[1]);
		// 设置数据库的登录用户密码
		dsWebgame.setPassword(conf.getString("dataSource.allurl").split("\\*\\*\\*")[2]);
		// 设置连接池的最大连接数
		dsWebgame.setMaxPoolSize(Integer.valueOf(conf.getString("dataSource.maxPoolSize")));
		// 设置连接池的最小连接数
		dsWebgame.setMinPoolSize(Integer.valueOf(conf.getString("dataSource.minPoolSize")));
		//设置连接池的初始化连接数
		dsWebgame.setInitialPoolSize(Integer.valueOf(conf.getString("dataSource.initialPoolSize")));
		
		dsWebgame.setTestConnectionOnCheckin(true);// 数据库断开后是否自动重连
		dsWebgame.setIdleConnectionTestPeriod(60);// 重连时间60秒

		dsWebgame.setMaxStatements(0);// 有时候会产生死锁，将maxStatements设置为0
		try {
			dsWebgame.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static long c=0;
	private static Lock  lock = new ReentrantLock();// 锁  
	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 1000; i++) {
			new Thread(){
				public void run() {
					System.out.println(new Date());
					//List list=BaseDao.getJdbcTemplate().queryForList("select id from micro_blog_user limit 0,100");
					//List list=BaseDao.getJdbcTemplate().queryForList("select id,title from zzy_seed limit 0,100");
					//List list1=Jdbc.find("select id from micro_blog_user limit 0,10");
//					lock.lock();
					c+=1;
//					lock.unlock();
					run();
				};
			}.start();
		}
		while (true) {
			System.err.println("num_connections: " + dsWebgame.getNumConnectionsDefaultUser());
			System.err.println("num_busy_connections: " + dsWebgame.getNumBusyConnectionsDefaultUser());
			System.err.println("num_idle_connections: " + dsWebgame.getNumIdleConnectionsDefaultUser());
			System.err.println("-------------------------"+c+"-----------------------------------");
			Thread.sleep(1000);
		}
	}
}
